# -*- coding: utf-8 -*-  
import pymongo
import sys
import unittest
reload(sys)
sys.setdefaultencoding('utf-8')

class mongo_store(object):
	def __init__(self,db_ip,db_port,db_name,db_table):   #初始化4个参数
		self.db_ip=db_ip
		self.db_port=db_port
		self.db_name=db_name
		self.db_table=db_table

		self.conn=pymongo.MongoClient(host=self.db_ip,port=self.db_port) #创建连接
		self.db=self.conn[self.db_name]   #数据库
		self.table=self.db[self.db_table]  #表（数据库中有多个表，用列表[]）

		'''
		增删改查
		'''
	def get_one(self,querry):
		return self.table.find_one(querry,projection=[{"_id":False}])


	def get_all(self,querry):
		return self.table.find(querry)


	def add(self,kv_dirt):
		print "adding···"
		return self.table.insert(kv_dirt)


	def delete(self,querry):
	
		return self.table.delete_many(querry)


	def update(self,querry,kv_dirt):
		res=self.table.update(
			querry,
			{"$set":kv_dirt

			})

		if not res.match_count or res.match_count==0:
			self.add(kv_dirt)
		
		elif res.match_count and res.match_count >1:
			self.delete(querry)
			self.add(kv_dirt)
			'''

class mongo_test(unittest.TestCase):
	
	def setUp(self):

		self.db_api=mongo_store("127.0.0.1",27017,"db_bug","table_bug")
		
	def test(self):
		db_api=self.db_api
		db_api.add({"id_":"2","project_name":"satis","bug_num":"102"})
		
	



if __name__ == '__main__':
	
	unittest.main()
	
			
'''